import lejos.nxt.*;
/**
 * A simple sonar sensor test program.
 * 
 * The sensor should be connected to port 1. In the
 * known bugs and limitations of leJOS NXJ version alfa_03
 * it is mentioned that a gap of at least 300 msec is 
 * needed between calls of getDistance. This is the reason 
 * for the delay of 300 msec between sonar readings in the 
 * loop.
 * 
 * @author  Ole Caprani
 * @version 30.08.07
 */
public class MicTest 
{

   public static void main(String [] args)  
   throws Exception 
   {
	   SoundSensor ss = new SoundSensor(SensorPort.S2);
	   int buttonPushedCounter = 1;
	   
	   DataLogger logger = new DataLogger("RecordedVolume1.csv", false);
	   logger.start();

       LCD.drawString("Volume(%): ", 0, 0);
       int volume = 0;
	   
       while (! Button.ESCAPE.isDown())
       {
    	   volume = ss.readValue();
           LCD.drawInt(volume,3,13,0);
           logger.writeSample(volume);

           Thread.sleep(300);
           if(Button.ENTER.isDown())
           {
        	   logger.close();
        	   logger = new DataLogger("RecordedVolume" + ++buttonPushedCounter +".csv", false);
        	   logger.start();
        	   LCD.drawInt(-1,3,13,0);
        	   Thread.sleep(2000);
           }
       }
       logger.close();
       LCD.clear();
       LCD.drawString("Program stopped", 0, 0);
       Thread.sleep(2000);
   }
}
